#include "../../include/session/SessionStorage.hpp"
using namespace http::session;

void MemorySessionStorage::save(std::shared_ptr<Session> session)
{
    _sessions[session->getSessionId()] = session;
}

std::shared_ptr<Session> MemorySessionStorage::load(const std::string &session_id)
{
    auto it = _sessions.find(session_id);
    if(it != _sessions.end())
    {
        std::shared_ptr<Session> session = it->second;
        // 如果会话过期，则删除会话
        if(!session->isExpired())
        {
            return session;
        }
        else
        {
            _sessions.erase(it);
        }
    }
    // 如果会话不存在或已过期，则返回空指针
    return nullptr;
}

void MemorySessionStorage::remove(const std::string &session_id)
{
    _sessions.erase(session_id);
}